package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatTask;
import com.happy.cat.project.system.service.ICatTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 日常任务Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/task" )
@Api("日常任务 API")
public class CatTaskController extends BaseController {

    private final ICatTaskService iCatTaskService;

    /**
     * 查询日常任务列表
     */
    @ApiOperation(value = "查询日常任务列表",notes = "查询日常任务列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatTask catTask)
    {
        startPage();
        LambdaQueryWrapper<CatTask> lqw = new LambdaQueryWrapper<CatTask>();
        if (StringUtils.isNotBlank(catTask.getName())){
            lqw.like(CatTask::getName ,catTask.getName());
        }
        if (catTask.getSticks() != null){
            lqw.eq(CatTask::getSticks ,catTask.getSticks());
        }
        if (catTask.getExperiebces() != null){
            lqw.eq(CatTask::getExperiebces ,catTask.getExperiebces());
        }
        if (catTask.getOnlines() != null){
            lqw.eq(CatTask::getOnlines ,catTask.getOnlines());
        }
        if (catTask.getCreateDate() != null){
            lqw.eq(CatTask::getCreateDate ,catTask.getCreateDate());
        }
        if (catTask.getStatus() != null){
            lqw.eq(CatTask::getStatus ,catTask.getStatus());
        }
        List<CatTask> list = iCatTaskService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出日常任务列表
     */
    @ApiOperation(value = "导出日常任务列表",notes = "导出日常任务列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:task:export')" )
    @Log(title = "日常任务" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatTask catTask) {
        LambdaQueryWrapper<CatTask> lqw = new LambdaQueryWrapper<CatTask>(catTask);
        List<CatTask> list = iCatTaskService.list(lqw);
        ExcelUtil<CatTask> util = new ExcelUtil<CatTask>(CatTask. class);
        return util.exportExcel(list, "task" );
    }

    /**
     * 获取日常任务详细信息
     */
    @ApiOperation(value = "获取日常任务详细信息",notes = "获取日常任务详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:task:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatTaskService.getById(id));
    }

    /**
     * 新增日常任务
     */
    @ApiOperation(value = "新增日常任务",notes = "新增日常任务",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:task:add')" )
    @Log(title = "日常任务" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatTask catTask) {

        catTask.setCreateDate(new Date());
        catTask.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatTaskService.save(catTask) ? 1 : 0);
    }

    /**
     * 修改日常任务
     */
    @ApiOperation(value = "修改日常任务",notes = "修改日常任务",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:task:edit')" )
    @Log(title = "日常任务" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatTask catTask) {
        catTask.setCreateDate(new Date());
        catTask.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatTaskService.updateById(catTask) ? 1 : 0);
    }

    /**
     * 删除日常任务
     */
    @ApiOperation(value = "删除日常任务",notes = "删除日常任务",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:task:remove')" )
    @Log(title = "日常任务" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatTaskService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
